//
//  CHMainTabBarController.swift
//  Weibo
//
//  Created by charmer on 16/9/17.
//  Copyright © 2016年 charmer. All rights reserved.
//

import UIKit

class CHMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //替换系统的tabbar，使用自定义的tabbar
        let newTabBar = CHMainTabBar()
        
        let composeCallback = {() -> Void in
        print("在控制器监听到加号按钮点击")
            
        }
        newTabBar.composeCallback = composeCallback
        
        // get-only的属性,只读属性. 不能使用 = 赋值, 可以使用KVC (键值编码),间接给属性赋值
        // value: 要设置的值
        // Key: 对象的属性的值
        self.setValue(newTabBar, forKey: "tabBar")
        
        
        self.addChildViewController(controller: CHHomeViewController(), title: "首页", imageNamed: "tabbar_home")

        self.addChildViewController(controller: CHDiscoverViewController(), title: "发现", imageNamed: "tabbar_discover")
        
        self.addChildViewController(controller: CHMessageViewController(), title: "消息", imageNamed: "tabbar_message_center")
        
        self.addChildViewController(controller: CHProfileViewController(), title: "我", imageNamed: "tabbar_profile")
        
//        //首页
//        let homeVC = CHHomeViewController()
//        homeVC.tabBarItem.image = UIImage(named: "tabbar_home")
//        homeVC.title = "首页"
//        tabbarVC.addChildViewController(UINavigationController(rootViewController: homeVC))
//        
//        //发现
//        let discoverVC = CHDiscoverViewController()
//        discoverVC.title = "发现"
//        discoverVC.tabBarItem.image = UIImage(named: "tabbar_discover")
//        tabbarVC.addChildViewController(UINavigationController(rootViewController: discoverVC))
//        
//        //消息
//        let messageVC = CHMessageViewController()
//        messageVC.title = "消息"
//        messageVC.tabBarItem.image = UIImage(named: "tabbar_message_center")
//        tabbarVC.addChildViewController(UINavigationController(rootViewController: messageVC))
//        
//        //我
//        let profileVC = CHProfileViewController()
//        profileVC.title = "我"
//        profileVC.tabBarItem.image = UIImage(named: "tabbar_profile")
//        tabbarVC.addChildViewController(UINavigationController(rootViewController: profileVC))

    }
    
    private func addChildViewController(controller: UIViewController, title: String, imageNamed: String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named:imageNamed)
        //选中的图片不需要渲染
        controller.tabBarItem.selectedImage = UIImage(named: imageNamed + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置tarbaritem选中的颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: UIControlState.selected)
        
        self.addChildViewController(UINavigationController(rootViewController: controller))
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
}
